#!/bin/bash
source /etc/os-release

if [ ${ID} == "centos" ]; then
  distro=${ID}
  sed -i "/^mirrorlist=.*/s/^/#/g" /etc/yum.repos.d/CentOS-*.repo
  if [ ${VERSION_ID} == "7" ]; then
    sed -i.bak "s|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.aliyun.com|g" /etc/yum.repos.d/CentOS-*.repo
  else
    sed -i.bak "s|^#baseurl=http://mirror.centos.org/\$contentdir|baseurl=https://mirrors.aliyun.com/centos|g" /etc/yum.repos.d/CentOS-*.repo
  fi
elif [ ${ID} == "rocky" ]; then
  sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
    -i.bak /etc/yum.repos.d/rocky*.repo
elif [ ${ID} == "almalinux" ]; then
  sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^# baseurl=https://repo.almalinux.org|baseurl=https://mirrors.aliyun.com|g' \
    -i.bak /etc/yum.repos.d/almalinux*.repo
elif [ ${ID} == "debian" ]; then
  if [ ${VERSION_ID} == "12" ]; then
    sed -i.bak "s|http://deb.debian.org|http://mirrors.aliyun.com|g" /etc/apt/sources.list.d/debian.sources
  else
    sed -i.bak "s|http://deb.debian.org|http://mirrors.aliyun.com|g" /etc/apt/sources.list
  fi
elif [ ${ID} == "ubuntu" ]; then
  sed -e "s|http://archive.ubuntu.com|http://mirrors.aliyun.com|g" \
    -e "s|http://security.ubuntu.com|http://mirrors.aliyun.com|g" \
    -i.bak /etc/apt/sources.list
else
  echo "Unsupported distributions"
fi
